package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClazzService clazzService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<Student> page(StudentQueryParam student) {
        Page<Student> pageList = Page.of(student.getPage(), student.getPageSize());

        pageList.addOrder(OrderItem.desc("update_time"));

        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(student.getName() != null && !student.getName().isEmpty(), Student::getName, student.getName()).eq(student.getDegree() != null, Student::getDegree, student.getDegree()).eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId());


        pageList = page(pageList, queryWrapper);


        //加载班级信息
        List<Student> students = pageList.getRecords();
        List<Long> clazzIds = students.stream().map(student1 -> student1.getClazzId()).collect(Collectors.toList());

        List<Clazz> clazzs = clazzService.listByIds(clazzIds);

        students.forEach(student1 -> {
            clazzs.forEach(clazz -> {
                if (student1.getClazzId().equals(clazz.getId())) {
                    student1.setClazzName(clazz.getName());
                }
            });
        });

        return new PageResult<>(pageList.getTotal(), pageList.getRecords());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        save(student);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Student getStudentById(Long id) {
        Student student = getById(id);
        return student;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeStudentBatchByIds(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void violation(Long id, Integer score) {
        Student student = getById(id);
        if(student == null){
            throw new RuntimeException("学号不存在");
        }
        if(student.getViolationScore() != null){
            student.setViolationCount(student.getViolationCount() + 1);
            student.setViolationScore(student.getViolationScore() + score);
        }else{
            student.setViolationCount(0);
            student.setViolationScore(score);
        }
        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }
}
